<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author neozilon
 */
class Admin_Form_Category extends Zend_Form{

    public function addCategory(){
        $this->setName('add_category_form');
        $this->setAction('/admin/category/add')
              ->setMethod('post');
        $this->setAttrib('id', 'category_user_form');
        $this->setIsArray(true);
        $this->setElementsBelongTo('category');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Nombre:')
                ->setRequired(true)
                ->setAttrib('class', 'text-input')
                ->setValue('')
                ->addValidator('notEmpty');

        $categoryObj = new Nosql_Model_Mysql_Category();
        $options = $categoryObj->getCategories();

        $parents = array('-1' => 'NINGUNA');
        if ($options){
            foreach ($options as $category){
                $parents[$category['categoryid']] = $category['name'];
            }
        }

        $parentId = new Zend_Form_Element_Select('parentId');
        $parentId->setLabel('Categoria Padre: ')
            ->setMultiOptions($parents);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel("Agregar Categoría")
                ->setAttrib('class', 'button')
                ->setAttrib('escape', false);

        $this->addElements(array($name, $parentId,$submit));

        $this->setElementDecorators(array(
                'viewHelper',
                'label',
                'errors',
                array('htmlTag',array('tag' =>'p'))

        ));

        $this->addDecorator('formElements');
        $this->submit->removeDecorator('Label');
        $this->addDecorator('form');

        return $this;
    }

    public function editCategory($categoryId){
        $this->setName('edit_category_form');
        $this->setAction('/admin/category/edit/category/' . $categoryId)
              ->setMethod('post');
        $this->setAttrib('id', 'edit_category_form');
        $this->setIsArray(true);
        $this->setElementsBelongTo('category');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Nombre:')
                ->setAttrib('class', 'text-input')
                ->setRequired(true)
                ->addValidator('notEmpty');

        $categoryObj = new Nosql_Model_Mysql_Category();
        $options = $categoryObj->getCategories();

        $parents = array('-1' => 'NINGUNA');
        if ($options){
            foreach ($options as $category){
                $parents[$category['categoryid']] = $category['name'];
            }
        }

        $parentId = new Zend_Form_Element_Select('parentId');
        $parentId->setLabel('Categoria Padre: ')
            ->setMultiOptions($parents);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Actualizar Categoría')
                ->setAttrib('class', 'button')
                ->setAttrib('escape', false);

        $this->addElements(array($name, $parentId,$submit));

        $this->setElementDecorators(array(
                'viewHelper',
                'label',
                'errors',
                array('htmlTag',array('tag' =>'p'))

        ));

        $this->addDecorator('formElements');
        $this->submit->removeDecorator('Label');
        $this->addDecorator('form');

        return $this;
    }

    public function searchCategory(){
        $this->setName('search_category_form');
        $this->setAction('/admin/category/search')
              ->setMethod('post');
        $this->setAttrib('id', 'search_category_form');
        $this->setIsArray(true);
        $this->setElementsBelongTo('category');

        $query = new Zend_Form_Element_Text('q');
        $query->setLabel('Texto:')
                ->setRequired(true)
                ->setAttrib('class', 'text-input');

        $fields = array(1  =>'Nombre',
                        2 =>'Id');
        $field = new Zend_Form_Element_Select('field');
        $field->setLabel('Buscar en campo:')
            ->setMultiOptions($fields);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Buscar Categoría')
                ->setAttrib('class', 'button')
                ->setAttrib('escape', false);

        $this->addElements(array($query, $field,$submit));

        $this->setElementDecorators(array(
                'viewHelper',
                'label',
                'errors',
                array('htmlTag',array('tag' =>'p'))

        ));

        $this->addDecorator('formElements');
        $this->submit->removeDecorator('Label');
        $this->addDecorator('form');

        return $this;
    }
}
